/**
 * Copyright (c) 2011 ~ 2013 Deepin, Inc.
 *               2011 ~ 2013 jouyouyun
 *
 * Author:      jouyouyun <jouyouwen717@gmail.com>
 * Maintainer:  jouyouyun <jouyouwen717@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 **/

package main

import (
	"fmt"
	"github.com/howeyc/fsnotify"
)

var (
	watcher *fsnotify.Watcher
	err     error
)

func watchFile(path string, isFile bool) {
	err = watcher.Watch(path)
	if err != nil {
		fmt.Printf("Watch '%s' Failed: %s\n", path, err)
		panic(err)
	}

	go func() {
		for {
			select {
			case ev := <-watcher.Event:
				fmt.Println("Watch Event:", ev)
				if isFile {
					if ev.IsDelete() {
						watcher.Watch(path)
					} else if ev.IsModify() {
                                                fmt.Println("File Changed")
                                        }
				}
			case err = <-watcher.Error:
				fmt.Println("Watch Error:", err)
			}
		}
	}()
}

func main() {
	watcher, err = fsnotify.NewWatcher()
	if err != nil {
		fmt.Println("Create Watcher Failed:", err)
		panic(err)
	}

	//watchFile("/home/caicai/.local/share/applications", false)
	//watchFile("/home/caicai/.local/share/applications/mimeapps.list", true)
        watchFile("/etc", false)

	select {}
}
